package cn.iocoder.yudao.module.system.service.standard;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.standard.vo.StandardPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.standard.vo.StandardSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.standard.StandardDO;
import cn.iocoder.yudao.module.system.dal.mysql.standard.StandardMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.DICT_DATA_NOT_EXISTS;


/**
 * 存储健康指标基准值，上下限通过计算规则动态生成 Service 实现类
 *
 * @author 安捷星
 */
@Service
@Validated
public class StandardServiceImpl implements StandardService {

    @Resource
    private StandardMapper standardMapper;

    @Override
    public Long createStandard(StandardSaveReqVO createReqVO) {
        // 插入
        StandardDO standard = BeanUtils.toBean(createReqVO, StandardDO.class);
        standardMapper.insert(standard);
        // 返回
        return standard.getId();
    }

    @Override
    public void updateStandard(StandardSaveReqVO updateReqVO) {
        // 校验存在
        validateStandardExists(updateReqVO.getId());
        // 更新
        StandardDO updateObj = BeanUtils.toBean(updateReqVO, StandardDO.class);
        standardMapper.updateById(updateObj);
    }

    @Override
    public void deleteStandard(Long id) {
        // 校验存在
        validateStandardExists(id);
        // 删除
        standardMapper.deleteById(id);
    }

    private void validateStandardExists(Long id) {
        if (standardMapper.selectById(id) == null) {
            throw exception(DICT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public StandardDO getStandard(Long id) {
        return standardMapper.selectById(id);
    }

    @Override
    public PageResult<StandardDO> getStandardPage(StandardPageReqVO pageReqVO) {
        return standardMapper.selectPage(pageReqVO);
    }

}